#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

//定义线程执行函数
void *func1 (void *arg) {
	printf ("func1 is running...\n");
	sleep (2);
	return  (void *)1;
}
void *func2 (void *arg) {
	printf ("func2 is running...\n");
	sleep (2);
	pthread_exit ((void *)2);
}

void *func3 (void *arg) {
	while (1) {
		printf ("func3 is running...\n");
		sleep (1);
	}
}

int main (void)
{
	pthread_t tid;
	void *ret;
	//创建线程
	pthread_create (&tid, NULL, func1, NULL);
	pthread_create (&tid, NULL, func2, NULL);
	pthread_create (&tid, NULL, func3, NULL);
	sleep (3);
	
	//给func3 线程发送取消请求
	pthread_cancel (tid);
	//等待线程终止
	pthread_join (tid, &ret);
	printf ("func3 exit code is %d\n", (int)ret);
	if (PTHREAD_CANCELED == ret) {printf ("haha!\n");}
	return 0;
}
